<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.name"
          clearable
          size="small"
          placeholder="角色名称"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation :crud="crud" />
      </div>
      <crudOperation :permission="permission" />
    </div>
    <!--表单组件-->
    <el-dialog
      append-to-body
      :before-close="crud.cancelCU"
      :title="crud.status.title"
      :visible.sync="crud.status.cu > 0"
      width="500px"
    >
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name" style="width: 370px;" />
        </el-form-item>
        <el-form-item label="角色级别" prop="level">
          <el-input v-model="form.level" style="width: 200px;" type="number" />
        </el-form-item>
        <el-form-item label="菜单权限">
          <el-tree
            :data="menus"
            show-checkbox
            ref="menu"
            node-key="id"
            empty-text="加载中，请稍后"
            :props="defaultProps"
            :default-checked-keys="menuIds"
          ></el-tree>
        </el-form-item>
        <el-form-item label="备注" prop="description">
          <el-input
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 8}"
            style="width: 370px;"
            placeholder="请输入内容"
            v-model="form.description"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.loading" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      default-expand-all
      style="width: 100%;"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="序号" type="index" width="200" />
      <el-table-column prop="name" label="角色名称" />
      <el-table-column prop="level" label="级别" />
      <el-table-column label="操作" width="130px" align="center" fixed="right">
        <template slot-scope="scope">
          <udOperation
            :data="scope.row"
            :permission="permission"
            :disabled-dle="scope.row.id === 1"
            msg="确定删除吗,如果存在下级节点则一并删除，此操作不能撤销！"
          />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import { getMenusTree } from '@/api/system/menu'
import crudRule from '@/api/table/role'
import Treeselect from '@riophae/vue-treeselect';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';
import CRUD, { presenter, header, form, crud } from '@crud/crud';
import rrOperation from '@crud/RR.operation';
import crudOperation from '@crud/CRUD.operation';
import udOperation from '@crud/UD.operation';
import pagination from '@crud/Pagination';

// crud交由presenter持有
const defaultCrud = CRUD({ title: '角色', url: 'api/role', crudMethod: { ...crudRule } });
const defaultForm = { name: null, level: null, description: null, id: null, label: null, value: null, dict: { id: null }, sort: 999, menus: [] };

export default {
  name: 'role',
  components: { Treeselect, crudOperation, rrOperation, udOperation, pagination },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      title: '角色详情',
      sort: ['sort,asc', 'id,desc'],
      menus: [],
      menuIds: [],
      permission: {
        add: ['admin', 'role:add'],
        edit: ['admin', 'role:edit'],
        del: ['admin', 'role:del']
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      },
      defaultProps: {
        children: "children",
        label: "label"
      },
    }
  },
  created() {
    this.getMenus()
    this.$nextTick(() => {
      this.crud.toQuery()
    })
  },
  methods: {
    [CRUD.HOOK.beforeToAdd]() {
      if (this.$refs.menu != undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.beforeToEdit](crud, form) {
      if (this.$refs.menu != undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
      crud.form.menus = [];
      let $this = this;
      crudRule.getRoleMenus(form.id).then(data => {
        data.menuIds.forEach(item => {
          $this.$refs.menu.setChecked(item, true);
        });
      })
    },
    // 获取所有菜单
    getMenus() {
      getMenusTree().then(res => {
        this.menus = res
      })
    },
    // 提交前做的操作
    [CRUD.HOOK.afterValidateCU](crud) {
      let amenus = [];
      let rid = crud.form.id;
      this.$refs.menu.getHalfCheckedKeys().forEach(function (data, index) {
        const menu = { id: data };
        amenus.push(menu);
      })
      this.$refs.menu.getCheckedKeys().forEach(function (data, index) {
        const menu = { id: data };
        amenus.push(menu);
      })
      crud.form.menus = amenus;
      return true;
    },
  }
}
</script>

<style lang="scss" scoped>
</style>

